package com.leetcode.sorts.tree;

/**
 * @Author: WuPeng
 * @Date: 2021/6/7 8:08 下午
 */
public class BalanceTree_110 {

    public boolean isBalance(TreeNode root){
        return getDepth(root) == -1 ? false : true;
    }

    private int getDepth(TreeNode node) {
        if (node == null) return 0;
        int left_depth = getDepth(node.left);
        if (left_depth == -1)
            return -1;
        int right_depth = getDepth(node.right);
        if (right_depth == -1)
            return -1;
        return Math.abs(left_depth - right_depth) > 1 ? -1:1+Math.max(left_depth, right_depth);
    }

}
